package com.situ.car.controller;

import com.situ.car.pojo.vo.BrandCountVo;
import com.situ.car.pojo.vo.OrderBrandCountVo;
import com.situ.car.pojo.vo.OrderCountVo;
import com.situ.car.service.ICarBrandService;
import com.situ.car.service.ICarService;
import com.situ.car.service.IOrderService;
import com.situ.car.service.IUserService;
import com.situ.car.service.Impl.CarBrandServiceImpl;
import com.situ.car.service.Impl.CarServiceImpl;
import com.situ.car.service.Impl.OrderServiceImpl;
import com.situ.car.service.Impl.UserServiceImpl;
import com.situ.car.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/echarts")
public class EchartsController extends BaseController{
    private ICarService carService = new CarServiceImpl();
    private IUserService userService = new UserServiceImpl();
    private IOrderService orderService = new OrderServiceImpl();
    private ICarBrandService carBrandService = new CarBrandServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("EchartsController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectCount" :
                selectCount(req, resp);
                break;
            case "selectBrandCount" :
                selectBrandCount(req, resp);
                break;
            case "selectTotalOrder" :
                selectTotalOrder(req, resp);
                break;
            case "selectOrderBrandCount" :
                selectOrderBrandCount(req, resp);
                break;
        }
    }

    private void selectOrderBrandCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("EchartsController.selectOrderBrandCount");
        List<OrderBrandCountVo> list = carBrandService.selectOrderBrandCount();
        toJSON(resp, list);
    }

    private void selectTotalOrder(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("EchartsController.selectTotalOrder");
        List<OrderCountVo> list = orderService.selectTotalOrder();
        toJSON(resp, list);
    }

    private void selectBrandCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("EchartsController.selectBrandCount");
        List<BrandCountVo> list = carBrandService.selectBrandCount();
        toJSON(resp, list);
    }

    private void selectCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("EchartsController.selecteCount");
        Integer totalCar = carService.totalCost();
        Integer totalBrand = carBrandService.totalCost();
        Integer totalUser = userService.totalCost();
        Integer totalOrder = orderService.totalCost();
        Map<String, Object> map = new HashMap<>();
        map.put("totalCar", totalCar);
        map.put("totalBrand", totalBrand);
        map.put("totalUser", totalUser);
        map.put("totalOrder", totalOrder);
        toJSON(resp, Result.ok(map));
    }
}
